package com.sky.shortlink.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.sky.shortlink.admin.convention.result.Result;
import com.sky.shortlink.admin.convention.result.Results;
import com.sky.shortlink.admin.dto.req.UserLoginReqDTO;
import com.sky.shortlink.admin.dto.req.UserRegisterReqDTO;
import com.sky.shortlink.admin.dto.req.UserUpdateReqDTO;
import com.sky.shortlink.admin.dto.resp.UserActorRespDTO;
import com.sky.shortlink.admin.dto.resp.UserLoginRespDTO;
import com.sky.shortlink.admin.dto.resp.UserRespDTO;
import com.sky.shortlink.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author Blue sky
 * @Description 用户控制层
 * @create 2024-02-15 15:25
 */

@RestController
@RequiredArgsConstructor  //使用构造方法注入
public class UserController {

    private final UserService userService;

    /**
     * 根据用户名查询用户
     */
    @GetMapping("/api/short-link/admin/v1/user/{username}")
    public Result<UserRespDTO> getUserByUsername(@PathVariable("username") String username){
        UserRespDTO result = userService.getUserByUsername(username);
//        result为时 直接在UserServiceImpl里面抛异常了，根本不会到达这里
//        if(result == null){
//            return new Result<UserRespDTO>().setCode(UserErrorCodeEnum.USER_NULL.code()).setMessage(UserErrorCodeEnum.USER_NULL.message());
//        }
        return Results.success(result);
    }

    /**
     * 根据用户名查询无脱敏的用户信息
     */
    @GetMapping("/api/short-link/admin/v1/actual/user/{username}")
    public Result<UserActorRespDTO> getActualUserByUsername(@PathVariable("username") String username){
        UserRespDTO result = userService.getUserByUsername(username);
        UserActorRespDTO result2 = BeanUtil.toBean(result, UserActorRespDTO.class);

        return Results.success(result2);
    }

    /**
     * 查询用户是否存在
     */
    @GetMapping("/api/short-link/admin/v1/user/has-username")
    public Result<Boolean> hasUsername(String username){
        Boolean result = userService.hasUsername(username);
        return Results.success(result);
    }


    /**
     * 用户注册
     */
    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO registerReqDTO){
        userService.register(registerReqDTO);
        return Results.success();
    }

    /**
     * 修改用户
     */
    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void> update(@RequestBody UserUpdateReqDTO updateReqDTO){
        userService.update(updateReqDTO);
        return Results.success();
    }

    /**
     * 用户登录
     */
    @PostMapping("/api/short-link/admin/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO loginReqDTO){
        return Results.success(userService.login(loginReqDTO));
    }


    /**
     * 检查用户是否登录
     */
    @GetMapping("/api/short-link/admin/v1/user/check-login")
    public Result<Boolean> check(@RequestParam("username") String username, @RequestParam("token")String token){
        return Results.success(userService.check(username,token));
    }
    /**
     * 用户退出登录
     */
    @DeleteMapping("/api/short-link/admin/v1/user/logout")
    public Result<Void> logout(@RequestParam("username") String username, @RequestParam("token")String token){
        userService.logout(username, token);
        return Results.success();
    }







}
